package com.pinionoffice

class User {

	transient springSecurityService

	String username
	String password
	boolean enabled
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired

    String email
    String phone
    String mobile
    String fax
    String address
    String zip

    Photo avatar
    Date lastLoginDate = new Date()

	static constraints = {
		username blank: false, unique: true
		password blank: false
        email size: 0..128, email: true, nullable: true
        phone size: 0..32, nullable: true
        mobile size: 0..32, nullable: true
        fax size: 0..32, nullable: true
        address size: 0..32, nullable: true
        zip size: 0..32, nullable: true
        avatar nullable: true
    }

	static mapping = {
		password column: '`password`'
	}

	Set<Role> getAuthorities() {
		UserRole.findAllByUser(this).collect { it.role } as Set
	}

	def beforeInsert() {
		encodePassword()
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService.encodePassword(password)
	}
}
